package com.exam.controller;


import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class BaseController {

	public static final String SESSION_KEY_LOGIN_USER = "_session:key:login:user:";

	protected static HttpServletRequest getRequest() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		return attrs.getRequest();
	}

	protected static HttpServletResponse getResponse() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		return attrs.getResponse();
	}

	protected static HttpSession getSession() {
		HttpServletRequest request = getRequest();
		HttpSession session = request.getSession(false);
		if (session == null) {
			session = request.getSession(true);
		}
		return session;
	}

//	protected Integer getLoginUid() {
//		Object u = getSession().getAttribute(SESSION_KEY_LOGIN_USER);
//		if (u instanceof User) {
//			return ((User) u).getId();
//		}
//		return null;
//	}
//
//	protected User getLoginUser() {
//		Object u = getSession().getAttribute(SESSION_KEY_LOGIN_USER);
//		if (u instanceof User) {
//			return (User) u;
//		}
//		return null;
//	}

}
